#pragma once
#include <unordered_map>
#include <vector>
#include <string>
#include <mutex>
#include <algorithm>

class HotFileManager {
public:
    void access(const std::string& filename) {
        std::lock_guard<std::mutex> lock(mtx);
        hot_map[filename]++;
    }
    std::vector<std::string> get_hot_files(size_t n = 5) {
        std::vector<std::pair<std::string, int> > vec(hot_map.begin(), hot_map.end());
        std::sort(vec.begin(), vec.end(), [](const std::pair<std::string, int>& a, const std::pair<std::string, int>& b){ return a.second > b.second; });
        std::vector<std::string> res;
        for(size_t i=0; i<n && i<vec.size(); ++i) res.push_back(vec[i].first);
        return res;
    }
private:
    std::unordered_map<std::string, int> hot_map;
    std::mutex mtx;
}; 